// import {waitClick, waitDisapper} from "./helper";

// waitClick('funcImage');

// waitClick();

// waitDisapper();

// waitClick();

// waitDisapper();

// waitClick();
function waitClick(name) {
    var el = desc(name).findOne();
    el.click();
}

function waitDisapper(name) {
    while (!desc(name).exists()) return;
}

console.log('脚本开始运行');

while (new Date().getHours() < 9);

let step = 0;

try {
    
    var nextConfirm = () => {
        try {
            if (desc('确认').exists()) {
                var el = desc('确认').findOne();
                el.click();
                waitClick('确认下单');
                step++;
                console.log('current', step);
            } else {
                setTimeout(() => {
                    nextConfirm();
                }, 20);
            }
        } catch (error) {
            console.log('error', error, step);
        }
    };
    
    console.log('开始抢购');
    if (desc('立即抢购').exists()) {
        console.log('发现元素');
    } else {
        console.log('未发现元素');
    }
    waitClick('立即抢购');
    step = 1;
    console.log('current', step);
    
    var count = 0;
    do {
        while (desc('退出排队').exists() || (!desc('退出排队').exists() && !desc('预约抢购').exists())) ;
        step = 2;
        
        count++;
        console.log('current', step, 'loop', count);
        waitClick('预约抢购');
    } while(desc('退出排队').exists());
    
    count = 0;
    do {
        waitClick('确认下单');
        step = 3;
        count;
        console.log('current', step, 'loop', count);
        if (desc('确认').exists()) {
            var el = desc('确认').findOne();
            el.click();
        }
    } while(desc('确认下单').exists());


} catch (error) {
    console.error('error', error, step);
}
